// packageD/mycomponent/invoice-pop/invoice-pop.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    popShow: false,
    explainShow: false,
    is_content: false,
    is_notice: false,

    invoice_type_index: 1, //发票类型
    invoice_type_arr: [],
    invoice_header_index: 1, //发票抬头
    invoice_header_arr: [{
      name: "个人",
      value: 1
    }, {
      name: "单位",
      value: 0
    }],
    invoice_content_index: 1, //发票内容
    invoice_content_arr: [],

    invoice_header_list: [], //发票抬头列表
    invoice_header_list_show: false,

    invoice_explain: "", //发票须知

    address: "", //地址，只有纸质发票跟专用发票才需要

    collect_name: "", //发票抬头 || 单位名称
    gmf_taxpayer: "", //纳税号
    gmf_bank: "", //开户银行
    gmf_bank_admin: "", //购买方银行账户
    gmf_address: "", //注册地址
    gmf_mobile: "", //注册电话
    col_mobile: "", //收票人手机号
    email: "", //电子邮箱

    inputFocusName: "", //判断当前那个输入框获得光标 用来显示输入框右边是否显示 清除 图标
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initData(data) {
      let {address, sinvoice} = data;
      if (sinvoice.is_open == 1 && sinvoice.invoice_type.length >= 1) {
        let changeData = {};
        changeData.popShow = true;
        changeData.invoice_type_arr = sinvoice.invoice_type;
        changeData.invoice_type_index = sinvoice.invoice_type[0].value;
        let content = sinvoice.content;
        content.push({
          name: "不开发票",
          value: 2
        });
        changeData.is_content = sinvoice.is_content;
        changeData.invoice_content_arr = content;
        changeData.invoice_content_index = changeData.invoice_content_arr[0].value;
        changeData.invoice_explain = sinvoice.notice;
        changeData.invoice_header_list = sinvoice.rises;
        let {personal, special, unit} = sinvoice.default_rise;
        changeData.personalRise = personal??{};
        changeData.specialRise = special??{};
        changeData.unitRise = unit??{};

        if (!this.data.address) {
          changeData.address = {
            realname: address.realname,
            mobile: address.mobile,
            col_address: address.address
          }
        }
        this.setData(changeData);
        this.setDefaultData(this.data.personalRise, 1);
      }
    },

    setDefaultData (defaultData, type) {
      let {
        col_address, col_mobile, col_name, collect_name,
        email, gmf_address, gmf_bank, gmf_bank_admin, gmf_mobile, gmf_taxpayer
      } = defaultData;
      let setData = {};
      if (type == 1) {
        // 个人       
        setData.collect_name = collect_name??'';
        setData.col_mobile = col_mobile??'';
        setData.email = email??'';
        setData.address = {};
      } else if (type == 2 || type == 3) {
        // 单位 || 专用
        setData.collect_name = collect_name??'';
        setData.gmf_taxpayer = gmf_taxpayer??'';
        setData.gmf_bank = gmf_bank??'';
        setData.gmf_bank_admin = gmf_bank_admin??'';
        setData.gmf_address = gmf_address??'';
        setData.gmf_mobile = gmf_mobile??'';
        setData.col_mobile = col_mobile??'';
        setData.email = email??'';

        setData.address = {
          realname: col_name??'',
          mobile: col_mobile??'',
          col_address: col_address??''
        };
      }
      this.setData(setData)
    },

    send() {
      let {
        invoice_type_index,
        invoice_header_index,
        invoice_content_index,
        collect_name,
        gmf_taxpayer,
        gmf_bank,
        gmf_bank_admin,
        gmf_address,
        gmf_mobile,
        col_mobile,
        email,
        address
      } = this.data;

      // 不开发票
      if (this.data.invoice_content_index == "2" && this.data.is_content) {
        this.setData({ popShow: false });
        return false;
      }
      // status和apply 后端要求写死
      let json = {
        status: 2,
        apply: 1,
        invoice_type: invoice_type_index,
        rise_type: invoice_header_index,
        content: invoice_content_index
      };

      if (invoice_type_index == 2 || invoice_type_index == 81) {
        json.rise_type = 0;
      }

      if (app._isTextEmpty(collect_name)) {
        if (invoice_header_index == 0 || invoice_type_index == 2 || invoice_type_index == 81) {
          return app.tips("请输入单位名称");
        } else {
          return app.tips("请输入发票抬头");
        }
      }
      json.collect_name = collect_name;


      //单位 或者 专用发票
      if (invoice_type_index == 2 || invoice_header_index == 0 || invoice_type_index == 81) {
        if (app._isTextEmpty(gmf_taxpayer)) return app.tips("请输入纳税人识别号");

        //专用发票要求必须输入，其他选选填
        if (invoice_type_index == 2 || invoice_type_index == 81) {
          if (app._isTextEmpty(gmf_bank)) return app.tips("请输入单位开户银行");
          if (app._isTextEmpty(gmf_bank_admin)) return app.tips("请输入单位银行账号");
          if (app._isTextEmpty(gmf_address)) return app.tips("请输入注册地址");
          if (app._isTextEmpty(gmf_mobile)) return app.tips("请输入注册电话");
        }

        json.gmf_taxpayer = gmf_taxpayer;

        json.gmf_bank = gmf_bank;
        json.gmf_bank_admin = gmf_bank_admin;
        json.gmf_address = gmf_address;
        json.gmf_mobile = gmf_mobile;
      }

      //非 电子发票 需要选择地址
      if (invoice_type_index != 0 && invoice_type_index != 82) {
        if (!address) return app.tips("请选择地址");
        json.col_address = address.col_address;
        json.col_name = address.realname;
        json.col_mobile = address.mobile;
      }

      //电子发票
      if (invoice_type_index == 0 || invoice_type_index == 82) {
        json.email = email;
        json.col_mobile = col_mobile;
      }
      this.triggerEvent("confirm", json);
      this.setData({
        popShow: false
      });
    },
    openAddList() {
      this.triggerEvent('openAddList');
    },
    setAddress(item) {
      let address = {};
      address.realname = item.username;
      address.mobile = item.mobile;
      address.col_address = `${item.province} ${item.city} ${item.district} ${item.street??''} ${item.address??''}`;
      this.setData({
        address
      });
    },
    setPopShow() {
      this.setData({
        popShow: !this.data.popShow
      })
    },
    setExplainShow() {
      this.setData({
        explainShow: !this.data.explainShow
      })
    },
    setTabsIndex(evt) {
      let {value, name} = evt.currentTarget.dataset;
      this.setData({ [name]: value })

      let {
        invoice_header_index, 
        invoice_type_index, 
        unitRise, 
        specialRise, 
        personalRise
      } = this.data;


      if (name == "invoice_type_index" && (invoice_type_index == 2 || invoice_type_index == 81)) {
        this.setDefaultData(specialRise, 3);
      } else if (invoice_header_index == 0) {
        this.setDefaultData(unitRise, 2);
      } else {
        this.setDefaultData(personalRise, 1);
      }
    },
    setInvoiceHeaderListShow() {
      this.setData({
        invoice_header_list_show: !this.data.invoice_header_list_show
      })
    },
    setInvoiceHeader(evt) {
      let content = evt.currentTarget.dataset.content;
      let {collect_name, tax_num, bank, bank_admin, address, mobile} = content;
      let setJson = {};
      if (this.data.invoice_header_index == 1) {
        setJson.collect_name = collect_name;
      } else {
        setJson = {
          collect_name: collect_name,
          gmf_taxpayer: tax_num,
          gmf_bank: bank,
          gmf_bank_admin: bank_admin,
          gmf_address: address,
          gmf_mobile: mobile,
        }
      }
      this.setData({
        invoice_header_list_show: false,
        ...setJson
      })
    },
    clearInputText(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        [name]: ""
      })
    },
    inputFocus(evt) {
      let name = evt.currentTarget.dataset.name;
      this.setData({
        inputFocusName: name
      })
    },
    inputBlur() {
      let name = this.data.inputFocusName;
      this.setData({
        [name]: this.data[name].replace(/\s+/g, ''),
        inputFocusName: ""
      })
    }
  }
})